#ifndef SEQUENCIALREADPAGES_H_
#define SEQUENCIALREADPAGES_H_

#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

class SequencialReadPages {
public:
	SequencialReadPages();
	~SequencialReadPages();
	bool next(void);
	string getPageURL();
	string getPageHTML();
	
	void setCheckpoint(void){
		infoTable->checkpoint = infoTable->lastReaded;
		infoTable->statusEndOfCurrentFile = endOfCurrentFile;
		infoTable->checkpoint_file_ID = infoTable->file_ID;
	};
	void goBackToCheckpoint(void){
		infoTable->nextToRead = infoTable->checkpoint;
		endOfCurrentFile = infoTable->statusEndOfCurrentFile;
		infoTable->file_ID = infoTable->checkpoint_file_ID;
		reloadFile(infoTable->file_ID);
		next(); //Automaticamente chama next() para carregar dados...
	};
	
	void reset(void){
		infoTable->file_ID = 0;
		endOfCurrentFile = false;
		infoTable->file_ID = 0;
		reloadFile(infoTable->file_ID);
		next();
	}
	
	//Terminar depois.... 
	unsigned long htmlDocID(void){
		unsigned long mask_ID = (unsigned long)(infoTable->lastReaded);  //trunca
		unsigned long mask;
		
		// infoTable->file_ID - usar para id ?
		// Colocar ID e colocar num var para numero de paginas lidas por ID
		
		mask = 0x1FFFFFFF; //doc zero, temporario ainda
		mask_ID &= mask;   //mascara
		return mask_ID;
	}
	
private:
	
	string fileName(int id);
	bool reloadFile(int id);
	bool nextTrap(void);
	
	unsigned int maxNumFiles; 
	FILE * arquivoDePaginas;
	unsigned int posTagInit, nextTagInit, posTagEnd;
	int tagInitSize;
	int tagEndSize;
	unsigned long long tempBufferSize;
	int totalReal;
	char *tempBuffer;
	string stringBuffer;
	string pageURL, pageHTML;
	struct _infoTable{
		unsigned long long nextToRead;
		unsigned long long lastReaded;
		unsigned long long checkpoint;
		bool statusEndOfCurrentFile;
		int file_ID;
		int checkpoint_file_ID;
	} *infoTable;
	
	bool endOfCurrentFile;
	unsigned long long fileSize;
	
};

#endif /*SEQUENCIALREADPAGES_H_*/
